from django.db import models


# Create your models here.
class Author(models.Model):
    name = models.CharField(max_length=50, null=False)
    UID = models.CharField(max_length=100,null=False)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '作者'
        verbose_name_plural = '作者'


class Category(models.Model):
    category_name = models.CharField(max_length=20, null=False, unique=True)

    def __str__(self):
        return self.category_name

    class Meta:
        verbose_name = '分类'
        verbose_name_plural = '分类'


class Product(models.Model):
    # 视频标题
    title = models.CharField(max_length=100, null=False)
    # BV
    BVID = models.CharField(max_length=100, null=False, unique=True)
    # 分类
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    # 视频地址
    video_url = models.URLField(max_length=200)
    # 作者
    author_list = models.ManyToManyField(Author, related_name='author_list')
    # 综合评分
    score_list = models.IntegerField()
    # 播放数
    play_cnt_list = models.IntegerField()
    # 弹幕数
    danmu_cnt_list = models.IntegerField()
    # 投币数
    coin_cnt_list = models.IntegerField()
    # 点赞数
    like_cnt_list = models.IntegerField()
    # 点踩数
    dislike_cnt_list = models.IntegerField()
    # 分享数
    share_cnt_list = models.IntegerField()
    # 收藏数
    favorite_cnt_list = models.IntegerField()
    # 评论数
    reply_cnt_list = models.IntegerField()

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = '视频'
        verbose_name_plural = '视频'


class Comment(models.Model):
    # 绑定Product
    BVID = models.ForeignKey(Product, on_delete=models.CASCADE)
    # 视频地址
    video_url_list = models.URLField(max_length=200)
    # 弹幕地址
    danmu_url_list = models.URLField(max_length=200)
    # 弹幕时间
    time_list = models.DateTimeField()
    # 弹幕出现时间
    time_loc_list = models.CharField(max_length=20)
    # 弹幕内容
    text_list = models.TextField(max_length=50)

    def shortcut_intro(self):
        if len(str(self.text_list)) > 20:
            return self.text_list[:20] + "..."
        else:
            return self.text_list

    # 设置截断的intro在排序时遵循原来的字段
    shortcut_intro.admin_order_field = 'text_list'
    # 给截断的intro设置别名
    shortcut_intro.short_description = "弹幕内容"

    def __str__(self):
        return self.text_list

    class Meta:
        verbose_name = '弹幕'
        verbose_name_plural = '弹幕'

